package com.yyk.yykywd.adapter;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yyk.yykywd.R;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.config.HttpUrl;

import java.util.List;

/**
 * 展示上传的图片的GridView的适配器
 * <p>
 * 作者： 周旭 on 2017/6/21/0021.
 * 邮箱：374952705@qq.com
 * 博客：http://www.jianshu.com/u/56db5d78044d
 * <p>
 * 上传人员列表
 */

public class MoreGridViewAdapter extends android.widget.BaseAdapter {

    private Context mContext;
    private List<MoreGvBean> mList;
    private LayoutInflater inflater;
    DeleteItemListenter listenter;

    public interface DeleteItemListenter {
        void onDeleteItemListenter(int postion);
    }

    public MoreGridViewAdapter(Context mContext, List<MoreGvBean> mList, DeleteItemListenter listenter) {
        this.mContext = mContext;
        this.mList = mList;
        this.listenter = listenter;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getCount() {
        //return mList.size() + 1;//因为最后多了一个添加图片的ImageView
        int count = mList == null ? 1 : mList.size() + 1;
        if (count > Constant.MAX_SELECT_PIC_NUM) {
            return mList.size();
        } else {
            return count;
        }
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        convertView = inflater.inflate(R.layout.gridview_item_morephoto, parent, false);
        final ImageView iv_photo = (ImageView) convertView.findViewById(R.id.iv_photo);
        final ImageView iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
        final TextView tv_type = (TextView) convertView.findViewById(R.id.tv_type);
        if (position < mList.size()) {
            tv_type.setText(mList.get(position).getType()); //这里可以优化 提示证件证件名字
            //代表+号之前的需要正常显示图片
            final String picUrl = mList.get(position).getPhoto(); //图片路径
            Glide.with(mContext).load(HttpUrl.BASE_URL + picUrl).into(iv_photo);
            iv_delete.setVisibility(View.VISIBLE);
            Log.i("适配器中的", mList.get(position).getPhoto());
            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("删除后的", mList.get(position).getPhoto());
                    listenter.onDeleteItemListenter(position);
                    mList.remove(position);
                    Glide.with(mContext).load(R.mipmap.icon_sctpbaba).into(iv_delete);
                    notifyDataSetChanged();
                }
            });
        } else {
            iv_photo.setImageResource(R.mipmap.icon_sctpbaba);//最后一个显示加号图片
        }
        return convertView;
    }


}

